namespace EyeOpen.Domain.Repositories
{
	using System;
	using System.Collections;

	using System.Collections.Generic;

	using System.Linq;

	using System.Linq.Expressions;

	using EyeOpen.Domain.Transactional;

	public class Repository<T>
		: IRepository<T>
		where T : class, IAggregateRoot
	{
		private readonly ITransactionalCollection session;
		private readonly IQueryable<T> query;

		public Repository(ITransactionalCollection session)
		{
			this.session = session;
			query = session.Query<T>();

			IsReadOnly = false;
			Expression = query.Expression;
			ElementType = query.ElementType;
			Provider = query.Provider;
		}

		public Expression Expression { get; private set; }

		public Type ElementType { get; private set; }

		public IQueryProvider Provider { get; private set; }

		public bool IsReadOnly { get; private set; }

		public IEnumerator<T> GetEnumerator()
		{
			return query.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		public virtual void Add(T entity)
		{
			session.Add(entity);
		}

		public virtual bool Remove(T entity)
		{
			session.Delete(entity);

			return true;
		}

		public virtual void Update(T entity)
		{
			session.Update(entity);
		}

		public T GetByKey(IComparable key)
		{
			return session.GetByKey<T>(key);
		}
	}
}